using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Globalization;
using System.Reflection;

namespace WinCANTool
{
       
    
    public partial class ParseSymbols
    {
        public struct EnumStruct
        {
            public string MessageName;
            public string Buffer;
        }

        bool Found;
        
        
        public void BuildOutFile(string FileName, ListBox ListIn, int First, int Last, string BaudRate, string HeaderText)
        {
            

            FileStream fs = new FileStream(FileName,FileMode.Create);
            StreamWriter newfile = new StreamWriter(fs);
            string DataBuffer = "CanTool Version = " + String.Format("Version {0}", AssemblyVersion) + "\r\n";
            DataBuffer += "Current Baud Rate = " + BaudRate + "\r\n\r\n";
            DataBuffer += HeaderText + "\r\n";
            List<EnumStruct> EnumList = new List<EnumStruct>();
           
            
            foreach (KeyValuePair<string, Message> MessageSymbol in MySymbols.NewSymbol)
            {
                DataBuffer += "[Message]" + "\r\n";
                DataBuffer += "Name =  " + MessageSymbol.Value.Name + "\r\n";
                DataBuffer += "Value =  0x" + MessageSymbol.Value.IdentValue.ToString("X") + "\r\n";
                DataBuffer += "Mask =  0x" + MessageSymbol.Value.Mask.ToString("X") +"\r\n";
                DataBuffer += "SendRate =  " + MessageSymbol.Value.SendRate.ToString() + "\r\n";
                DataBuffer += "Length =  " + MessageSymbol.Value.Length.ToString() + "\r\n";
                DataBuffer += "[End]" + "\r\n" + "\r\n";

            }

            foreach (KeyValuePair<string, SubMessage> SubMessageTemp in MySymbols.NewSubId)
            {
                DataBuffer += "[SubMessage]" + "\r\n";
                DataBuffer += "Name =  " + SubMessageTemp.Value.SubMessageName + "\r\n";
                DataBuffer += "Mask =  0x" + SubMessageTemp.Value.Mask.ToString("X") + "\r\n";
                DataBuffer += "LinkedMessage =  " + SubMessageTemp.Value.MessageId + "\r\n";
                DataBuffer += "[End]" + "\r\n" + "\r\n";

            }

            foreach (KeyValuePair<string, DataWord> DataWordTemp in MySymbols.NewData)
            {
                DataBuffer += "[DataWord]" + "\r\n";
                DataBuffer += "Name =  " + DataWordTemp.Value.Name + "\r\n";
                DataBuffer += "LinkedMessage =  " + DataWordTemp.Value.MessageId + "\r\n";
                DataBuffer += "LinkedDataEnum =  " + DataWordTemp.Value.LinkedData + "\r\n";
                DataBuffer += "WordType =  " + DataWordTemp.Value.WordType + "\r\n";
                if (DataWordTemp.Value.EndianType != "Big")
                    DataBuffer += "EndianType = Little \r\n";
                else
                    DataBuffer += "EndianType =  " + DataWordTemp.Value.EndianType + "\r\n";
                DataBuffer += "Start =  " + DataWordTemp.Value.StartByte.ToString() + "\r\n";
                DataBuffer += "SubIndexStart =  " + DataWordTemp.Value.SubIndexStartByte.ToString() + "\r\n";
                DataBuffer += "Length =  " + DataWordTemp.Value.WordLength.ToString() + "\r\n";
                DataBuffer += "[End]" + "\r\n" + "\r\n";


            }

            foreach (KeyValuePair<string, EnumIndex> EnumDataTemp in MySymbols.NewEnum)
            {

                string SymbolName = EnumDataTemp.Value.DataWordName;
                int Index = 0;
                Found = false;
                EnumStruct ElementCopy = new EnumStruct();

                foreach(EnumStruct Element in EnumList)
                {
                    if ((string.Compare(SymbolName, Element.MessageName)) == 0)
                    {
                        ElementCopy = Element;
                        ElementCopy.Buffer += "Index =  " + EnumDataTemp.Value.Name + 
                            ", 0x" + EnumDataTemp.Value.Index.ToString("X") + "\r\n";
                        Index = EnumList.IndexOf(Element);
                        Found = true;
                        break;
                    }
                }

                if (Found)
                {
                    EnumStruct tempstruct = new EnumStruct();
                    tempstruct = EnumList[Index];
                    EnumList.Remove(tempstruct);
                    EnumList.Add(ElementCopy);
                }
                else 
                {
                    EnumStruct NewElement = new EnumStruct();
                    NewElement.MessageName = SymbolName;

                    NewElement.Buffer = "[EnumIndex]" + "\r\n";
                    NewElement.Buffer += "Name =  " + SymbolName + "\r\n";
                    NewElement.Buffer += "Index =  " + EnumDataTemp.Value.Name +
                        ", 0x" + EnumDataTemp.Value.Index.ToString("X") + "\r\n";
                    EnumList.Add(NewElement);
                }
            }

            foreach (EnumStruct Element in EnumList)
            {
                DataBuffer += Element.Buffer + "[End]" + "\r\n" + "\r\n";
            }

            DataBuffer += "[EndSymbols]" + "\r\n" + "\r\n";
            DataBuffer += "[MessageData]" + "\r\n" + "\r\n";


            for (int i = First; ((i < ListIn.Items.Count) && (i < Last)) ; i++)
            {
                DataBuffer += ListIn.Items[i].ToString() + "\r\n";

            }

            DataBuffer +=  "\r\n" + "[End]" + "\r\n";

            newfile.WriteLine(DataBuffer);
            newfile.Close();
            fs.Close();
        
        }


        public string AssemblyVersion
        {
            get
            {
                return Assembly.GetExecutingAssembly().GetName().Version.ToString();
            }
        }



    }

}